package com.antaohua.tools.system.controller;

import com.antaohua.tools.system.entity.SysRoleEntity;
import com.antaohua.tools.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.antaohua.framework.web.controller.BaseController;
import com.antaohua.framework.web.utils.JsonResult;
import com.antaohua.tools.framework.base.exception.InsertEntityException;
import com.antaohua.tools.framework.base.exception.QureyEntityException;
import com.antaohua.tools.system.entity.SysUserEntity;
import com.antaohua.tools.system.service.SysUserService;

import java.util.List;

@Controller
@RequestMapping("sys/user")
public class SysUserController extends BaseController<SysUserEntity>{

	@Autowired
	private SysRoleService roleService;

	@RequestMapping(path="login")
	public @ResponseBody JsonResult login(String email,String password) {
		try {
			SysUserService userService = ((SysUserService)service);
			SysUserEntity user = userService.login(email, password);
			//user.setRole(null);
			if(user!=null){
				user.setPassword(null);
				return new JsonResult(JsonResult.STATUS_SUCCESS,user);
			}else{
				return new JsonResult(JsonResult.STATUS_ERROR,"用户名密码错误");
			}
			
			
			
		} catch (QureyEntityException e) {
			return new JsonResult(JsonResult.STATUS_ERROR,"服务器错误，请联系管理员");
		}
		
	}
	@RequestMapping(path="signup")
	public @ResponseBody JsonResult signup(String email,String password,String name){

		SysUserEntity user = new SysUserEntity();
		user.setPassword(password);
		user.setUserEmail(email);
		user.setUserName(name);
		try {
			//List<SysRoleEntity> roles = roleService.findByProperty("code","user");
			//user.setRoleId(roles.get(0).getId());
			service.save(user);
			return new JsonResult(JsonResult.STATUS_SUCCESS,user);
			
		} catch (InsertEntityException e) {
			return new JsonResult(JsonResult.STATUS_ERROR,"服务器错误，请联系管理员");
		}
	}
	
	
}
